package com.banyuan.lesson.网络编程;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String msg = null;
        try {
            ServerSocket server = new ServerSocket(8080);
            System.out.println("服务器已启动，正在监听，等待客户端连接！");
            //等待客户端连接，处于阻塞状态；
            Socket socket = server.accept();
            System.out.println("客户端已连接！");
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out.println("欢迎光临半圆服务器！");
            System.out.println(br.readLine());
            new Thread(() -> {
                while (true) out.println(sc.nextLine());
            }).start();
            while (true) {
                System.out.println(br.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
